package org.jeecg.modules.workflow.convert.acct.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.bcimp.entity.BusAcctInfo;
import com.boc.ewr.bcimp.service.IBusAcctInfoService;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.business.entity.CaUnpriaccApply;
import com.boc.ewr.util.file.enumeration.BusAccountStatusEnum;
import com.boc.ewr.util.file.enumeration.YnEnum;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.workflow.vo.acct.unpri.BpmAcctUnpriBaseVo;
import org.jeecg.common.workflow.vo.acct.unpri.BpmAcctUnpriRespVo;
import org.jeecg.modules.workflow.convert.acct.BpmAcctUnpriConvert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : Wzz
 * @create : 2023-04- 27- 11:03
 * @desc :
 */
@Service
public class BpmAcctUnpriConvertImpl implements BpmAcctUnpriConvert {

    @Autowired
    private IBusAcctInfoService busAcctInfoService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public CaUnpriaccApply convert(BpmAcctUnpriBaseVo bean) {
        if (bean == null) {
            return null;
        }
        CaUnpriaccApply caUnpriaccApply = new CaUnpriaccApply();
        BeanUtils.copyProperties(bean, caUnpriaccApply);
        caUnpriaccApply.setApplyDate(new Date());

        return caUnpriaccApply;
    }

    @Override
    public BpmAcctUnpriRespVo convert(CaUnpriaccApply bean) {
        if (bean == null) {
            return null;
        }
        BpmAcctUnpriRespVo bpmAcctUnpriRespVo = new BpmAcctUnpriRespVo();

        BeanUtils.copyProperties(bean, bpmAcctUnpriRespVo);
        bpmAcctUnpriRespVo.setCreateTime(bean.getApplyDate());

        return bpmAcctUnpriRespVo;
    }

    @Override
    public BpmAcctUnpriRespVo getInfoByUnpriaccNo(String unpriaccNo) {
        BpmAcctUnpriRespVo bpmAcctUnpriRespVo = new BpmAcctUnpriRespVo();

        BusAcctInfo busAcctInfo = busAcctInfoService.getOne(
                new LambdaQueryWrapper<BusAcctInfo>()
                        .eq(BusAcctInfo::getAccNo, unpriaccNo)
        );

        bpmAcctUnpriRespVo.setAdminOrgNo(busAcctInfo.getParentBranchNo());
        bpmAcctUnpriRespVo.setCusmNm(busAcctInfo.getCusmNm());
        bpmAcctUnpriRespVo.setCusmNo(busAcctInfo.getCusmNo());
        bpmAcctUnpriRespVo.setObranchNo(busAcctInfo.getBranchNo());

        if (BusAccountStatusEnum.CLOSE.getIndex().equals(busAcctInfo.getAcctSta()) ||
                BusAccountStatusEnum.IMMOVABLE.getIndex().equals(busAcctInfo.getAcctSta())) {
            bpmAcctUnpriRespVo.setIsCloseAcct(YnEnum.YES.getIndex());
        }else {
            bpmAcctUnpriRespVo.setIsCloseAcct(YnEnum.NO.getIndex());
        }

        return bpmAcctUnpriRespVo;
    }

    @Override
    public List<BpmAcctUnpriRespVo> convertList(List<CaUnpriaccApply> list) {
        if (list == null) {
            return null;
        }

        List<BpmAcctUnpriRespVo> list1 = new ArrayList<BpmAcctUnpriRespVo>(list.size());

        for (CaUnpriaccApply caUnpriaccApply : list) {
            list1.add(convert(caUnpriaccApply));
        }

        return list1;
    }

    @Override
    public IPage<BpmAcctUnpriRespVo> convertPage(IPage<CaUnpriaccApply> page) {
        if (page == null) {
            return null;
        }

        IPage<BpmAcctUnpriRespVo> pageResult = new Page<>();

        pageResult.setRecords(convertList(page.getRecords()));
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }
}
